<?php
/**
 *
 * User: Jessica@aweb.wang
 * Date: 2023/3/10 14:50
 * Email: <1194323013@qq.com>
 *                            _ooOoo_
 *                           o8888888o
 *                           88" . "88
 *                           (| -_- |)
 *                           O\  =  /O
 *                        ____/`---'\____
 *                      .'  \\|     |//  `.
 *                     /  \\|||  :  |||//  \
 *                    /  _||||| -:- |||||-  \
 *                    |   | \\\  -  /// |   |
 *                    | \_|  ''\---/''  |   |
 *                    \  .-\__  `-`  ___/-. /
 *                  ___`. .'  /--.--\  `. . __
 *               ."" '<  `.___\_<|>_/___.'  >'"".
 *              | | :  `- \`.;`\ _ /`;.`/ - ` : | |
 *              \  \ `-.   \_ __\ /__ _/   .-` /  /
 *         ======`-.____`-.___\_____/___.-`____.-'======
 *                            `=---='
 *        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 *                      Buddha Bless, No Bug !
 **/

namespace app\common\middleware;


use app\common\exception\BusinessException;
use app\common\exception\BusinessExceptionEnum;

use app\common\utils\TokenUtils;
use think\facade\Cookie;
use think\facade\Session;
use think\facade\View;

class BtApi extends Logger
{
    /**
     * 处理请求
     *
     * @param \think\Request $request
     * @param \Closure       $next
     * @return Response
     */
    public function handle($request, \Closure $next)
    {
        $token=$request->header("JWTToken");

        if(!strpos($request->url(),"login")){
            $tokenUtils=new TokenUtils();
            $flag=$tokenUtils->verifyToken($token);
            if($flag==false){
                throw new BusinessException(BusinessExceptionEnum::JWT_WRONG);
            }else{
                $request->userInfo=$tokenUtils->getDecodePayload($token);
            }
        }
        //$request->userInfo=["openId"=>1,"memberId"=>1];
        $response = $next($request);
        return $response;
    }

}